
/*
 *  Copyright 2007 Marc Lanctot and Marc Gendron-Bellemare
 *
 *  This file is part of OpenMTG.
 *
 *  OpenMTG is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenMTG is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenMTG.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


import java.io.*;
import java.util.*;
import java.net.*;

public class BlackLotus {
  public static final int ARG_GUI=1;
  public static final int ARG_HELP=0;
  public static final int ARG_PLAYER=2;

  public static void main(String[] args) throws IOException {

    // TODO: parse args for port
    int port = 4444;

    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket( port );
    } catch (IOException e) {
      System.err.println("Could not listen on port: "+port);
      System.exit(1);
    }

    Socket clientSocket = null;
    try {
      clientSocket = serverSocket.accept();
    } catch (IOException e) {
      System.err.println("Accept failed.");
      System.exit(1);
    }

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));

    // deal with command line args
    int i=0;
    while(i<args.length){
      switch( arg2int(args[i]) ){
      case ARG_HELP:
        System.out.println("Help Screen:");
        System.exit(0);
        break;
      case ARG_GUI:
        System.out.println("GUI type set to..");
        break;
      case ARG_PLAYER:
        System.out.println("Player options set to..");
        break;
      default:
        System.err.println("Problem with argument "+(i+1)+":");
        System.err.println("  "+args[i]);
        System.err.println("Use -help for a list of options.");
        System.exit(-1);
      }
      i++;
    }


    // test Deck
    String files[]={"decks/test.deck","decks/test.deck"};
    Engine e = new Engine( files );


    /**	for(int iii=0;iii<40;iii++){
        System.out.println(d.deal(0));
        }*/

    // network stuff
    String inputLine, outputLine;

    // initiate conversation with client
    BlackLotusProtocol blp = new BlackLotusProtocol();
    outputLine = blp.processInput(null);
    out.println(outputLine);

    while ((inputLine = in.readLine()) != null) {
      outputLine = blp.processInput(inputLine);
      out.println(outputLine);
      if (outputLine.equals("Bye."))
        break;
    }
    out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();


  }   // end main

  // arg2int's purpose is simply to let us use a switch(arg[i])
  public static int arg2int(String arg){
    if( arg.equalsIgnoreCase("-help") || arg.equalsIgnoreCase("-h") )
      return ARG_HELP;
    if( arg.equalsIgnoreCase("-gui") )
      return ARG_GUI;
    if( arg.equalsIgnoreCase("-player") )
      return ARG_PLAYER;

    return -1;
  }   // end arg2int

} // end class Main
